package com.forum.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.forum.model.entity.Post;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface PostMapper extends BaseMapper<Post> {
    @Insert("INSERT INTO post (user_id, title, category_id, content, summary, cover_image, status, created_at) " +
            "VALUES (#{userId}, #{title}, #{categoryId}, #{content}, #{summary}, #{coverImage}, #{status}, #{createdAt})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(Post post);

    @Select("SELECT p.*, u.username as author_name, u.picture as author_avatar, c.name as category_name " +
            "FROM post p " +
            "JOIN user u ON p.user_id = u.id " +
            "JOIN category c ON p.category_id = c.id " +
            "WHERE p.status = 1 " +
            "ORDER BY (p.views * 0.4 + p.likes * 0.4 + p.comments * 0.2) DESC " +
            "LIMIT 10")
    List<Post> findHotPosts();

    @Select("SELECT p.*, u.username as author_name, u.picture as author_avatar, c.name as category_name " +
            "FROM post p " +
            "JOIN user u ON p.user_id = u.id " +
            "JOIN category c ON p.category_id = c.id " +
            "WHERE p.status = 1 " +
            "ORDER BY p.created_at DESC " +
            "LIMIT 10")
    List<Post> findLatestPosts();

    @Select("SELECT p.*, u.username as author_name, u.picture as author_avatar " +
            "FROM post p " +
            "JOIN user u ON p.user_id = u.id " +
            "WHERE p.id = #{id}")
    Post selectById(Long id);

    @Update("UPDATE post SET views = views + 1 WHERE id = #{id}")
    int incrementViews(Long id);

    @Update("UPDATE post SET likes = likes + 1 WHERE id = #{id}")
    int incrementLikes(Long id);

    @Update("UPDATE post SET likes = GREATEST(likes - 1, 0) WHERE id = #{id}")
    int decrementLikes(Long id);

    @Update("UPDATE post SET comments = comments + 1 WHERE id = #{id}")
    void incrementComments(Long id);

    @Update("UPDATE post SET comments = comments - #{count} WHERE id = #{id}")
    void decrementComments(Long id, int count);

    @Update("UPDATE post SET comments = #{count} WHERE id = #{id}")
    void updateComments(Long id, int count);

    @Select("SELECT * FROM post WHERE user_id = #{userId} ORDER BY created_at DESC")
    List<Post> selectByUserId(Long userId);

    @Select("SELECT * FROM post WHERE category_id = #{categoryId} ORDER BY created_at DESC")
    List<Post> selectByCategoryId(Long categoryId);

    @Select("<script>" +
            "SELECT * FROM post WHERE id IN " +
            "<foreach collection='postIds' item='id' open='(' separator=',' close=')'>" +
            "#{id}" +
            "</foreach>" +
            " ORDER BY created_at DESC" +
            "</script>")
    List<Post> selectByIds(@Param("postIds") List<Long> postIds);

    @Update("UPDATE post SET title=#{title}, category_id=#{categoryId}, content=#{content}, " +
            "summary=#{summary}, cover_image=#{coverImage}, status=#{status}, updated_at=#{updatedAt} " +
            "WHERE id=#{id}")
    int updateById(Post post);
    @Update("UPDATE post SET title=#{title}, category_id=#{categoryId}, content=#{content}, " +
            "summary=#{summary}, cover_image=#{coverImage}, status=#{status}, updated_at=#{updatedAt} ,ai_audit_result=#{aiAuditResult} , ai_confidence=#{aiConfidence} , ai_suggestion=#{aiSuggestion} " +
            "WHERE id=#{id}")
    int updateByIds(Post post);
    @Delete("DELETE FROM post WHERE id = #{id}")
    int deleteById(Long id);

    /**
     * 查询待审核的帖子
     * @return 待审核帖子列表
     */
    @Select("SELECT p.*, u.username as author_name, u.picture as author_avatar, c.name as category_name " +
            "FROM post p " +
            "JOIN user u ON p.user_id = u.id " +
            "JOIN category c ON p.category_id = c.id " +
            "WHERE p.status = 2 " +
            "ORDER BY p.created_at ASC")
    List<Post> findPendingPosts();
    
    /**
     * 查询特定状态的帖子
     * @param status 帖子状态
     * @return 特定状态的帖子列表
     */
    @Select("SELECT p.*, u.username as author_name, u.picture as author_avatar, c.name as category_name " +
            "FROM post p " +
            "JOIN user u ON p.user_id = u.id " +
            "JOIN category c ON p.category_id = c.id " +
            "WHERE p.status = #{status} " +
            "ORDER BY p.created_at DESC")
    List<Post> findPostsByStatus(@Param("status") Integer status);
    
    /**
     * 更新帖子状态
     * @param id 帖子ID
     * @param status 新状态
     * @return 影响行数
     */
    @Update("UPDATE post SET status = #{status}, updated_at = NOW() WHERE id = #{id}")
    int updateStatus(@Param("id") Long id, @Param("status") Integer status);
    
    /**
     * 获取用户所有帖子，包括各种状态
     * @param userId 用户ID
     * @return 用户的所有帖子
     */
    @Select("SELECT p.*, c.name as category_name " +
            "FROM post p " +
            "LEFT JOIN category c ON p.category_id = c.id " +
            "WHERE p.user_id = #{userId} " +
            "ORDER BY p.created_at DESC")
    List<Post> selectAllByUserId(@Param("userId") Long userId);
} 